﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DisplayBoardInfo.Helpers
{
    class Convert
    {
        /// <summary>
        /// Converts <c>System.Drawing.Image</c> to array of bytes.
        /// </summary>
        /// <param name="imageToConvert">Image to convert</param>
        /// <param name="formatOfImage">Format of image</param>
        /// <returns>Array of bytes which contains coded image</returns>
        public static byte[] ImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
        {
            byte[] Ret;

            try
            {

                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    imageToConvert.Save(ms, formatOfImage);
                    Ret = ms.ToArray();
                }
            }
            catch (Exception) { throw; }

            return Ret;
        }

        /// <summary>
        /// Converts array of bytes to <c>System.Drawing.Image</c>.
        /// </summary>
        /// <param name="image">Array of bytes with coded image.</param>
        /// <returns>Image in </returns>
        public static Image ByteArrayToImage(byte[] image)
        {
            System.Drawing.Image newImage = null;

            try
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(image, 0, image.Length))
                {

                    ms.Write(image, 0, image.Length);

                    newImage = System.Drawing.Image.FromStream(ms, true);

                }
            }
            catch (NullReferenceException)
            {

            }

            return newImage;
        }

        /// <summary>
        /// Resizes <c>System.Drawing.Image</c>
        /// </summary>
        /// <param name="image">Image to resize</param>
        /// <param name="size">Expected size of image</param>
        /// <returns>Resized image</returns>
        public static Image ResizeImage(Image image, Size size)
        {
            Image img = (Image)image.Clone();

            //calculate the size of the image
            Size imgSize = GenerateImageDimensions(img.Width, img.Height, size.Width, size.Height);

            //create a new Bitmap with the proper dimensions
            Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);

            ////create a new Graphics object from the image
            //Graphics gfx = Graphics.FromImage(img);

            ////clean up the image (take care of any image loss from resizing)
            //gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

            return finalImg;
        }

        private static Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {
            //double to hold the final multiplier to use when scaling the image
            double multiplier = 0;

            //string for holding layout
            string layout;

            //determine if it's Portrait or Landscape
            if (currH > currW) layout = "portrait";
            else layout = "landscape";

            switch (layout.ToLower())
            {
                case "portrait":
                    //calculate multiplier on heights
                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }

                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }
                    break;
                case "landscape":
                    //calculate multiplier on widths
                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }

                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }
                    break;
            }

            //return the new image dimensions
            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }
    }
}
